//
//  SearchBarViewController.m
//  逗逼电影
//
//  Created by 王义凯 on 14-10-10.
//  Copyright (c) 2014年 wangyikai. All rights reserved.
//

#import "SearchBarViewController.h"

#import "Reachability.h"
#import <SystemConfiguration/SystemConfiguration.h>
#import "MBProgressHUD.h"
#import "NetHandler.h"
#import "NightModel.h"
#import "WebColor.h"

@interface SearchBarViewController ()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic ,retain)UISearchBar *searchBar;
@property (nonatomic ,retain)UITableView *tableView;
@property (nonatomic ,retain)NSMutableArray *showArray;

@property (nonatomic, retain) CityWeather *city;
@end

@implementation SearchBarViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.array = [NSMutableArray array];
        self.searchArray = [NSMutableArray array];
        self.showArray = [NSMutableArray array];
        
    }
    return self;
}
- (void)dealloc
{
  
    [_searchArray release];
    [_searchBar release];
    [_tableView release];
    [_showArray release];
    [_city release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    // Do any additional setup after loading the view.

    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];

    [self.view addSubview:_searchBar];
    [self.searchBar becomeFirstResponder];
    [self.searchBar setShowsCancelButton:YES animated:YES];
    [self.searchBar sizeToFit];
    self.searchBar.delegate = self;
    self.searchBar.placeholder = @"搜索城市";
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 548) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    if ([NightModel shareInstance].open == 1) {
        self.tableView.backgroundColor = [WebColor dimGray];
    } else {
        self.tableView.backgroundColor = [UIColor whiteColor];
    }
    
    
    self.tableView.tableHeaderView = self.searchBar;
    [self.view addSubview:_tableView];
    [_searchBar release];
    [_tableView release];
  
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"首页" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarAction:)];
 
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [self.showArray removeAllObjects];
    for (NSString *str in self.searchArray) {
        NSRange range = [str rangeOfString:searchText];
        if (range.length != 0) {
            [self.showArray addObject:str];
            
        }
        [self.tableView reloadData];  
    }
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    [self.navigationController popViewControllerAnimated:NO];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.showArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"] autorelease];
    }
    cell.textLabel.text = [self.showArray objectAtIndex:indexPath.row];
    if ([NightModel shareInstance].open == 1) {
        cell.backgroundColor = [WebColor dimGray];
        self.tableView.backgroundColor = [WebColor dimGray];
    } else {
        self.tableView.backgroundColor = [UIColor whiteColor];
    }
    

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BOOL reachable = [[Reachability reachabilityForInternetConnection] isReachable];
    
    if (!reachable) {
        UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"请检查您的网络连接状态" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] autorelease];
        [alertView show];
    } else {
    NSString *string = @"http://iphone.myzaker.com/zaker/get_weather.php?city=";
    
    NSString *str = self.showArray[indexPath.row];
    
    NSString *netStr = [string stringByAppendingString:str];
    [NetHandler networkWithURL:netStr comaletion:^(id result) {
        NSDictionary *dic = [NSDictionary dictionary];
        dic = [[result objectForKey:@"data"] objectForKey:@"datas"];
        self.city = [[CityWeather alloc] init];
        self.city.city = [dic objectForKey:@"city"];
        self.city.treal = [dic objectForKey:@"treal"];
        self.city.weather_day = [dic objectForKey:@"weather_day"];
        self.city.weather_night = [dic objectForKey:@"weather-night"];
        self.city.tmax = [dic objectForKey:@"tmax"];
        self.city.tmin = [dic objectForKey:@"tmin"];
    }];
    }
}
//  网络情况判断
-(BOOL)isReachableViaWWAN
{
    return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.searchBar resignFirstResponder];
}


- (void)rightBarAction:(UIBarButtonItem *)bar
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center postNotificationName:@"weather" object:self.city];
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
